package com.ie.controller;



import com.ie.common.http.Result;
import com.ie.common.http.ResultCodeEnum;
import com.ie.entity.dto.UsersDto;
import com.ie.entity.vo.UserVo;
import com.ie.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.image.BufferedImage;

@RestController
@RequestMapping("/system/users")
public class UsersController
{
    @Autowired
    private UsersService usersService;

    /**
     * 注册
     *
     * @param usersDto
     * @return
     */
    @PostMapping("/register")
    public Result registerUser(@RequestBody UsersDto usersDto) {
        usersService.registerUser(usersDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 登录
     * @param usersDto
     * @return
     */
    @PostMapping("/login")
    public Result loginUser(@RequestBody UsersDto usersDto) {
        System.out.println("usersDto = " + usersDto);
        UserVo userVo = usersService.loginUser(usersDto);
        return Result.build(userVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 验证码
     * @return
     */
    @GetMapping("/getCode")
    public Result getCode() {
        BufferedImage code = usersService.getCode();
        return Result.build(code, ResultCodeEnum.SUCCESS);
    }

    /**
     * 验证码验证
     * @param usersDto
     */
    @PostMapping("/ifCode")
    public Result ifCode(@RequestBody UsersDto usersDto) {
        usersService.ifCode(usersDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/getUserInfo/{id}")
    public Result getUserMessage(@PathVariable("id") Integer id) {
        UserVo userMessage = usersService.getUserMessage(id);
        return Result.build(userMessage, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改用户信息
     *
     * @return
     * @RequestBody UsersDto usersDto
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UsersDto usersDto) {
        usersService.updateUser(usersDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
